const jwt = require('jsonwebtoken');
const userService = require('../service/user.service')
const { PRIVATE_KEY } = require('../app/config');

class UserController {
  // 创建用户
  async create(ctx, next) {
    // 结构数据
    const { account, password } = ctx.request.body;
    const data = await userService.create({ account, password });
    ctx.response.body = data;
  }

  async login(ctx, next) {
    const { account, password } = ctx.request.body;
    const data = await userService.login({ account, password });
    const token = jwt.sign(
      { account, password },
      PRIVATE_KEY,
      {
        expiresIn: "1d",
        algorithm: "RS256"
      }
    )

    ctx.response.body = { "account": data[0].account, token };
  }
}

module.exports = new UserController()